﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.SlotJoint.CApSlotJointApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Tecnomatix.Engineering;
using UiInternalControls;


namespace DnProcessSimulateCommands.SlotJoint;

public class CApSlotJointApp : INotifyPropertyChanged
{
  private TxVector _pin1Value;
  private TxVector _pin2Value;
  private TxVector _offsetPointValue;
  private ITx1DimensionalGeometry _leadingCurve1;
  private ITx1DimensionalGeometry _leadingCurve2;
  private bool _leadingCurve1Selected;
  private bool _leadingCurve2Selected;
  private bool _pin1Selected;
  private bool _pin2Selected;
  private bool _offsetPointSelected;
  private bool _curve1Set;
  private bool _curve2Set;
  private bool _pin1Set;
  private bool _pin2Set;
  private bool _offsetPointSet;
  private bool _useOffsetPoint;
  private string _message;
  private MessageTypes _messageType;
  private SlotJointType _jointType;

  public CApSlotJointApp(bool useOffsetPoint)
  {
    this._pin1Value = new TxVector();
    this._pin2Value = new TxVector();
    this._offsetPointValue = new TxVector();
    this._useOffsetPoint = useOffsetPoint;
  }

  public ITx1DimensionalGeometry LeadingCurve1
  {
    get => this._leadingCurve1;
    set
    {
      if (this._leadingCurve1 != null && this._leadingCurve1 != this._leadingCurve2)
        TxDisplayableObjectEx.StopEmphasizeYellowObject(this._leadingCurve1 as ITxDisplayableObject);
      this._leadingCurve1 = value;
      if (this._leadingCurve1 != null && this._leadingCurve1 != this._leadingCurve2)
        TxDisplayableObjectEx.EmphasizeYellowObject(this._leadingCurve1 as ITxDisplayableObject);
      this.OnPropertyChanged(nameof (LeadingCurve1));
      this.UpdateIsDataReadyProperty();
    }
  }

  public ITx1DimensionalGeometry LeadingCurve2
  {
    get => this._leadingCurve2;
    set
    {
      if (this._leadingCurve2 != null && this._leadingCurve1 != this._leadingCurve2)
        TxDisplayableObjectEx.StopEmphasizeYellowObject(this._leadingCurve2 as ITxDisplayableObject);
      this._leadingCurve2 = value;
      if (this._leadingCurve2 != null && this._leadingCurve1 != this._leadingCurve2)
        TxDisplayableObjectEx.EmphasizeYellowObject(this._leadingCurve2 as ITxDisplayableObject);
      this.OnPropertyChanged(nameof (LeadingCurve2));
      this.UpdateIsDataReadyProperty();
    }
  }

  public TxVector Pin1Value
  {
    get => this._pin1Value;
    set
    {
      this._pin1Value = value;
      this.OnPropertyChanged(nameof (Pin1Value));
      this.UpdateIsDataReadyProperty();
    }
  }

  public TxVector Pin2Value
  {
    get => this._pin2Value;
    set
    {
      this._pin2Value = value;
      this.OnPropertyChanged(nameof (Pin2Value));
      this.UpdateIsDataReadyProperty();
    }
  }

  public TxVector OffsetPointValue
  {
    get => this._offsetPointValue;
    set
    {
      this._offsetPointValue = value;
      this.OnPropertyChanged(nameof (OffsetPointValue));
      this.UpdateIsDataReadyProperty();
    }
  }

  public bool LeadingCurve1Selected
  {
    get => this._leadingCurve1Selected;
    set
    {
      this._leadingCurve1Selected = value;
      if (this._leadingCurve1Selected)
      {
        this._leadingCurve2Selected = this._pin1Selected = this._pin2Selected = this._offsetPointSelected = false;
        this.OnPropertyChanged("Pin1Selected");
        this.OnPropertyChanged("Pin2Selected");
        this.OnPropertyChanged("OffsetPointSelected");
        this.OnPropertyChanged("LeadingCurve2Selected");
      }
      this.OnPropertyChanged(nameof (LeadingCurve1Selected));
    }
  }

  public bool LeadingCurve2Selected
  {
    get => this._leadingCurve2Selected;
    set
    {
      this._leadingCurve2Selected = value;
      if (this._leadingCurve2Selected)
      {
        this._leadingCurve1Selected = this._pin1Selected = this._pin2Selected = this._offsetPointSelected = false;
        this.OnPropertyChanged("Pin1Selected");
        this.OnPropertyChanged("Pin2Selected");
        this.OnPropertyChanged("OffsetPointSelected");
        this.OnPropertyChanged("LeadingCurve1Selected");
      }
      this.OnPropertyChanged(nameof (LeadingCurve2Selected));
    }
  }

  public bool Pin1Selected
  {
    get => this._pin1Selected;
    set
    {
      this._pin1Selected = value;
      if (this._pin1Selected)
      {
        this._leadingCurve1Selected = this._leadingCurve2Selected = this._pin2Selected = this._offsetPointSelected = false;
        this.OnPropertyChanged("LeadingCurve1Selected");
        this.OnPropertyChanged("LeadingCurve2Selected");
        this.OnPropertyChanged("Pin2Selected");
        this.OnPropertyChanged("OffsetPointSelected");
      }
      this.OnPropertyChanged(nameof (Pin1Selected));
    }
  }

  public bool Pin2Selected
  {
    get => this._pin2Selected;
    set
    {
      this._pin2Selected = value;
      if (this._pin2Selected)
      {
        this._leadingCurve1Selected = this._leadingCurve2Selected = this._pin1Selected = this._offsetPointSelected = false;
        this.OnPropertyChanged("LeadingCurve1Selected");
        this.OnPropertyChanged("LeadingCurve2Selected");
        this.OnPropertyChanged("Pin1Selected");
        this.OnPropertyChanged("OffsetPointSelected");
      }
      this.OnPropertyChanged(nameof (Pin2Selected));
    }
  }

  public bool OffsetPointSelected
  {
    get => this._useOffsetPoint && this._offsetPointSelected;
    set
    {
      this._offsetPointSelected = value;
      if (this._offsetPointSelected)
      {
        this._leadingCurve1Selected = this._leadingCurve2Selected = this._pin1Selected = this._pin2Selected = false;
        this.OnPropertyChanged("LeadingCurve1Selected");
        this.OnPropertyChanged("LeadingCurve2Selected");
        this.OnPropertyChanged("Pin1Selected");
        this.OnPropertyChanged("Pin2Selected");
      }
      this.OnPropertyChanged(nameof (OffsetPointSelected));
    }
  }

  public bool Pin1Set
  {
    get => this._pin1Set;
    set
    {
      this._pin1Set = value;
      this.OnPropertyChanged(nameof (Pin1Set));
    }
  }

  public bool Pin2Set
  {
    get => this._pin2Set;
    set
    {
      this._pin2Set = value;
      this.OnPropertyChanged(nameof (Pin2Set));
    }
  }

  public bool OffsetPointSet
  {
    get => this._offsetPointSet;
    set
    {
      this._offsetPointSet = value;
      this.OnPropertyChanged(nameof (OffsetPointSet));
    }
  }

  public bool LeadingCurve1Set
  {
    get => this._curve1Set;
    set
    {
      this._curve1Set = value;
      this.OnPropertyChanged(nameof (LeadingCurve1Set));
    }
  }

  public bool LeadingCurve2Set
  {
    get => this._curve2Set;
    set
    {
      this._curve2Set = value;
      this.OnPropertyChanged(nameof (LeadingCurve2Set));
    }
  }

  public bool UseOffsetPoint
  {
    get => this._useOffsetPoint;
    set
    {
      this._useOffsetPoint = value;
      this.OnPropertyChanged(nameof (UseOffsetPoint));
      this.UpdateIsDataReadyProperty();
    }
  }

  public string Message
  {
    get => this._message;
    set
    {
      this._message = value;
      this.OnPropertyChanged(nameof (Message));
    }
  }

  public MessageTypes MessageType
  {
    get => this._messageType;
    set
    {
      this._messageType = value;
      this.OnPropertyChanged(nameof (MessageType));
    }
  }

  public bool IsDataReady
  {
    get
    {
      if (!this.LeadingCurve1Set || !this.Pin1Set || !this.Pin2Set || (this.UseOffsetPoint ? (this.OffsetPointSet ? 1 : 0) : 1) == 0)
        return false;
      return this.SelectedSlotJointType != 1 || this.LeadingCurve2Set;
    }
  }

  public int SelectedSlotJointType
  {
    get => (int) this._jointType;
    set
    {
      this._jointType = (SlotJointType) value;
      this.OnPropertyChanged(nameof (SelectedSlotJointType));
    }
  }

  public bool ErrorExists => this.MessageType != MessageTypes.None && this.Message != string.Empty;

  public bool CreateSlotJoint(ITxManagedDialogSlotJoint manager)
  {
    manager.LeadingCurve1 = (ITxObject) this.LeadingCurve1;
    manager.Pin1 = this.TryProjectOnCurvePlane(this.Pin1Value);
    manager.Pin2 = this.TryProjectOnCurvePlane(this.Pin2Value);
    if (this.UseOffsetPoint)
      manager.OffsetPoint = this.TryProjectOnCurvePlane(this.OffsetPointValue);
    string str1;
    bool slotJoint;
    if (this.SelectedSlotJointType == 0)
    {
      slotJoint = manager.CreateOneSlotJoint(ref str1);
    }
    else
    {
      manager.LeadingCurve2 = (ITxObject) this.LeadingCurve2;
      slotJoint = manager.CreateTwoSlotsJoint(ref str1);
    }
    string str2 = string.Empty;
    MessageTypes messageTypes = MessageTypes.None;
    if (!slotJoint)
    {
      str2 = str1;
      messageTypes = MessageTypes.Error;
    }
    this.Message = str2;
    this.MessageType = messageTypes;
    return slotJoint;
  }

  public bool ValidatePinPosition(TxVector pinProjected, bool validatePin1)
  {
    bool flag = false;
    if (this.SelectedSlotJointType == 0)
      flag = this.ValidatePinPositionOnSingleSlot(pinProjected, validatePin1);
    else if (this.SelectedSlotJointType == 1)
      flag = this.ValidatePinPositionOnTwoSlots(pinProjected, validatePin1);
    return flag;
  }

  public bool ValidatePinPositionOnTwoSlots(TxVector pinProjected, bool validatePin1)
  {
    bool flag = false;
    string str1 = string.Empty;
    MessageTypes messageTypes = MessageTypes.None;
    if (TxVector.op_Inequality(pinProjected, (TxVector) null))
    {
      TxVector pin2;
      string str2;
      ITx1DimensionalGeometry curve;
      string str3;
      if (validatePin1)
      {
        pin2 = this.TryProjectOnCurvePlane(this.Pin2Value);
        str2 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Pin1Diagram;
        curve = this._leadingCurve1;
        str3 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve1;
      }
      else
      {
        pin2 = this.TryProjectOnCurvePlane(this.Pin1Value);
        str2 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Pin2Diagram;
        curve = this._leadingCurve2;
        str3 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve2;
      }
      if (curve == null || !CApSlotJointUtils.IsPointOnCurve(pinProjected, curve))
        str1 = string.Format(DnProcessSimulateCommands.SlotJoint.Resources.StringTable.PinNotOnCurve, (object) str2, (object) str3);
      else if ((validatePin1 ? (this.Pin2Set ? 1 : 0) : (this.Pin1Set ? 1 : 0)) != 0)
      {
        if (CApSlotJointUtils.ArePointsIdentical(pinProjected, pin2))
          str1 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.IdenticalPins;
        else
          flag = true;
      }
      else
        flag = true;
      if (!flag)
        messageTypes = MessageTypes.Warning;
    }
    this.Message = str1;
    this.MessageType = messageTypes;
    return flag;
  }

  public bool ValidatePinPositionOnSingleSlot(TxVector pinProjected, bool validatePin1)
  {
    bool flag = false;
    if (TxVector.op_Inequality(pinProjected, (TxVector) null))
    {
      string str1 = string.Empty;
      MessageTypes messageTypes = MessageTypes.None;
      TxVector txVector1;
      TxVector txVector2;
      TxVector txVector3;
      string str2;
      string str3;
      if (validatePin1)
      {
        txVector1 = this.TryProjectOnCurvePlane(this.Pin2Value);
        txVector2 = pinProjected;
        txVector3 = txVector1;
        str2 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Pin1Diagram;
        str3 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Pin2Diagram;
      }
      else
      {
        txVector1 = this.TryProjectOnCurvePlane(this.Pin1Value);
        txVector2 = txVector1;
        txVector3 = pinProjected;
        str2 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Pin2Diagram;
        str3 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Pin1Diagram;
      }
      bool isOnFirstSegment1;
      if (this._leadingCurve1 == null || !CApSlotJointUtils.IsPointOnCurveEndSegment(pinProjected, this._leadingCurve1, out isOnFirstSegment1))
        str1 = string.Format(DnProcessSimulateCommands.SlotJoint.Resources.StringTable.PinNotOnCurveFirstSegment, (object) str2);
      else if ((validatePin1 ? (this.Pin2Set ? 1 : 0) : (this.Pin1Set ? 1 : 0)) != 0)
      {
        if (CApSlotJointUtils.ArePointsIdentical(pinProjected, txVector1))
        {
          str1 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.IdenticalPins;
        }
        else
        {
          bool isOnFirstSegment2;
          if (CApSlotJointUtils.IsPointOnCurveEndSegment(txVector1, this._leadingCurve1, out isOnFirstSegment2) && isOnFirstSegment1 != isOnFirstSegment2)
            str1 = string.Format(DnProcessSimulateCommands.SlotJoint.Resources.StringTable.PinsNotOnSameCurveSegment, (object) str2, (object) str3);
          else if (!CApSlotJointUtils.IsPoint1CloserToCurveStartOrEndPoint(txVector2, txVector3, this._leadingCurve1, isOnFirstSegment1))
            str1 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.PinsWrongOrder;
          else if (!this.CanPin2ReachAllPointsOfLeadingCurve(txVector2, txVector3, isOnFirstSegment1))
            str1 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.InsufficientDistanceBetweenPins;
          else
            flag = true;
        }
      }
      else
        flag = true;
      if (!flag)
        messageTypes = MessageTypes.Warning;
      this.Message = str1;
      this.MessageType = messageTypes;
    }
    return flag;
  }

  public bool ValidateLeadingCurve(bool validateCurve1)
  {
    bool flag = false;
    string str1;
    string str2;
    ITx1DimensionalGeometry curve;
    ITx1DimensionalGeometry otherCurve;
    if (validateCurve1)
    {
      str1 = this.SelectedSlotJointType == 0 ? DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve : DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve1;
      str2 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve2;
      curve = this._leadingCurve1;
      otherCurve = this._leadingCurve2;
    }
    else
    {
      str1 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve2;
      str2 = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve1;
      curve = this._leadingCurve2;
      otherCurve = this._leadingCurve1;
    }
    if (curve != null)
    {
      if (this.SelectedSlotJointType == 0)
      {
        if (CApSlotJointUtils.IsCurveLine(curve))
          this.Message = string.Format(DnProcessSimulateCommands.SlotJoint.Resources.StringTable.InvalidCurveLinear, (object) DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurve);
        else
          flag = true;
      }
      else if (this.SelectedSlotJointType == 1)
      {
        if (otherCurve != null)
        {
          if (((ITxObject) curve).Id == ((ITxObject) otherCurve).Id)
            this.Message = string.Format(DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurveAlreadySelected, (object) ((ITxObject) curve).Name, (object) str2);
          else if (!CApSlotJointUtils.AreCurvesOnSamePlane(curve, otherCurve))
            this.Message = string.Format(DnProcessSimulateCommands.SlotJoint.Resources.StringTable.LeadingCurvesNotOnSamePlane, (object) str1, (object) str2);
          else
            flag = true;
        }
        else
          flag = true;
      }
    }
    if (!flag)
      this.MessageType = MessageTypes.Warning;
    return flag;
  }

  private bool CanPin2ReachAllPointsOfLeadingCurve(
    TxVector pin1,
    TxVector pin2,
    bool arePinsOnFirstSegment)
  {
    bool flag = true;
    double distance = CApSlotJointUtils.CalculateDistance(pin1, pin2);
    TxVector[] points = ((ITxGeometry) this._leadingCurve1).Approximation.Points;
    TxVector p1 = arePinsOnFirstSegment ? points[1] : points[((IEnumerable<TxVector>) points).Count<TxVector>() - 2];
    for (int index = 2; flag && index < points.Length; ++index)
      flag = CApSlotJointUtils.CalculateDistance(p1, points[index]) < distance;
    return flag;
  }

  public void UpdateIsDataReadyProperty()
  {
    this.OnPropertyChanged("IsDataReady");
    if (this.IsDataReady)
    {
      this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.Ready;
      this.MessageType = MessageTypes.None;
    }
    else
    {
      if (this.ErrorExists)
        return;
      if (!this.LeadingCurve1Set)
      {
        if (this.SelectedSlotJointType == 0)
        {
          this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.SelectCurve;
          this.MessageType = MessageTypes.None;
        }
        else
        {
          if (this.SelectedSlotJointType != 1)
            return;
          this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.SelectCurve1;
          this.MessageType = MessageTypes.None;
        }
      }
      else if (!this.Pin1Set)
      {
        this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.SelectPin1;
        this.MessageType = MessageTypes.None;
      }
      else if (this.SelectedSlotJointType == 1 && !this.LeadingCurve2Set)
      {
        this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.SelectCurve2;
        this.MessageType = MessageTypes.None;
      }
      else if (!this.Pin2Set)
      {
        this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.SelectPin2;
        this.MessageType = MessageTypes.None;
      }
      else if (!this.OffsetPointSet)
      {
        this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.SelectOffset;
        this.MessageType = MessageTypes.None;
      }
      else
        this.ClearErrorMessage();
    }
  }

  public bool ValidateOffsetPoint(TxVector point)
  {
    bool flag = false;
    if (this.SelectedSlotJointType == 0)
      flag = CApSlotJointUtils.IsPointOnPlaneOfCurve(point, this._leadingCurve1);
    else if (this.SelectedSlotJointType == 1)
      flag = CApSlotJointUtils.IsPointOnPlaneOfCurves(point, this._leadingCurve1, this._leadingCurve2);
    if (!flag)
    {
      this.Message = DnProcessSimulateCommands.SlotJoint.Resources.StringTable.OffsetPointNotOnSamePlaneAsCurve;
      this.MessageType = MessageTypes.Warning;
    }
    return flag;
  }

  public void ClearErrorMessage()
  {
    if (!string.IsNullOrEmpty(this.Message))
      this.Message = string.Empty;
    if (this.MessageType == MessageTypes.None)
      return;
    this.MessageType = MessageTypes.None;
  }

  public TxVector ProjectPointOnCurvePlane(TxVector inputVector)
  {
    if (TxVector.op_Equality(inputVector, (TxVector) null))
      return (TxVector) null;
    TxPlaneData txPlaneData = this.LeadingCurve1 == null || this.SelectedSlotJointType != 0 ? CApSlotJointUtils.GetCurvesPlaneData(this.LeadingCurve1, this.LeadingCurve2) : CApSlotJointUtils.GetCurvePlaneData(this.LeadingCurve1);
    return TxPlaneData.op_Equality(txPlaneData, (TxPlaneData) null) ? (TxVector) null : txPlaneData.ProjectPoint(inputVector);
  }

  public TxVector TryProjectOnCurvePlane(TxVector inputVector)
  {
    return this.ProjectPointOnCurvePlane(inputVector) ?? inputVector;
  }

  public void ClearSelection()
  {
    this._leadingCurve1Selected = this._leadingCurve2Selected = this._pin1Selected = this._pin2Selected = this._offsetPointSelected = false;
    this.OnPropertyChanged("Pin1Selected");
    this.OnPropertyChanged("Pin2Selected");
    this.OnPropertyChanged("OffsetPointSelected");
    this.OnPropertyChanged("LeadingCurve1Selected");
    this.OnPropertyChanged("LeadingCurve2Selected");
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }
}
